package cn.neusoft.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.neusoft.domain.Operator;
import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.service.LoginService;
import cn.neusoft.util.RandomUtil;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "/login", produces = "application/json;charset=utf-8")
@Api(value = "登录的API", description = "登录相关的功能")
public class LoginController {

    @Autowired
    LoginService operatorService;

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @PostMapping("/loginByPwd")
    @ApiOperation("通过账号密码登录")
    public ResultBean loginByPwd(@RequestBody Map<String,String> map){
        String account = map.get("no");
        String password =  map.get("password");
        if(StringUtils.isEmpty(account)||StringUtils.isEmpty(password)){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }
        Map<String, Object> mmp = operatorService.loginByPwd(account,password);

        // 会话登录
        StpUtil.login(account);

        // 获取 Token  相关参数
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        mmp.put("token", tokenInfo);

        return ResultBean.success(mmp);
    }

    @PostMapping("/sendSms")
    @ApiOperation("发送短信验证码")
    public ResultBean sendSms(@RequestBody Map<String,String> map){
        if (map == null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        System.out.println("======================"+map);

        String tel = map.get("tel");

        if (StringUtils.isEmpty(tel)){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        Operator operator = operatorService.findByTel(tel);

        if (operator == null){
            throw new AppException(AppExceptionCodeMsg.OPERATOR_TEL_NOT_EXITS);
        }

        String Vcode = RandomUtil.getSixBitRandom();
        String[] params = {Vcode, "5"};
        operatorService.send(tel, params);

        //存入redis，设置验证码5分钟失效
        redisTemplate.opsForValue().set(tel, Vcode,5, TimeUnit.MINUTES);
        return ResultBean.success();
    }


    @PostMapping("/loginByTel")
    @ApiOperation("通过手机验证码登录")
    public ResultBean loginByTel(@RequestBody Map<String,String> map){
        if (map == null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        String tel = map.get("tel");
        String code = map.get("code");

        if (StringUtils.isEmpty(tel) || StringUtils.isEmpty(code)){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        Map<String, Object> map1 = operatorService.loginByTel(tel, code);

        Operator operator = (Operator) map1.get("user");

        // 会话登录
        StpUtil.login(operator.getOperator_account());

        // 获取 Token  相关参数
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        map1.put("token", tokenInfo);

        return ResultBean.success(map1);
    }

    @GetMapping("isLogin")
    @ApiOperation("判断是否登录")
    public SaResult isLogin() {
        return SaResult.ok("是否登录：" + StpUtil.isLogin());
    }

    // 查询 Token 信息  ---- http://localhost:8081/acc/tokenInfo
    @GetMapping("tokenInfo")
    @ApiOperation("查询token信息")
    public SaResult tokenInfo() {
        return SaResult.data(StpUtil.getTokenInfo());
    }

    // 测试注销  ---- http://localhost:8081/acc/logout
    @GetMapping("logout")
    @ApiOperation("注销登录")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok();
    }

}
